//
//  CYLoanTableVC.m
//  CreditAddressBook
//
//  Created by LE on 15/11/3.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYLoanTableVC.h"
#import "CYMeHttpTool.h"
#import "CYPayBackTableVC.h"
#import "CYLoginHttpTool.h"
#import "CYEscape.h"
#import "CYLoanSectionView.h"
#import "CYLoanDetailCell.h"
#import "CYTool.h"

@interface CYLoanTableVC ()<CYLoanSectionViewDelegate>

@property(nonatomic, strong) NSArray *datas;
@property(nonatomic, strong) CYLoanSectionView *openedSectionView;
@end

@implementation CYLoanTableVC

- (NSArray *)datas {
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableView];
    [self loadData];
}

- (void)loadData {
    CYUser *user = [CYLoginHttpTool userData].user;
    NSParameterAssert(user);
    if (user) {
        NSDictionary *par = @{@"userId": [CYEscape escapeWithString:@"1"], @"token": [CYEscape escapeWithString:user.token]};
        [CYMeHttpTool loanListWithParams:par success:^(NSArray *arr) {
            self.datas = arr;
            
            [self.tableView reloadData];
        } failure:^(NSError *error) {
            
        }];
    } else {
        //跳转到登录
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CYLoanSectionView *sectionHeaderView = (CYLoanSectionView *)[tableView headerViewForSection:section];
    if (sectionHeaderView) {
        return sectionHeaderView.cartBtn.selected ? 1 : 0;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *ID = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
//    }
//    NSDictionary *a = self.datas[indexPath.row];
//    cell.textLabel.text = a[@"name"];
//    NSNumber *status = a[@"status"];
//    if (status.intValue == 1) {
//        cell.detailTextLabel.text = @"已还清";
//    } else {
//        cell.detailTextLabel.text = @"未还清";
//    }
//    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", a[@"status"]];
    
    CYLoanDetailCell *cell = [CYLoanDetailCell loanDetailCell:tableView];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *a = self.datas[indexPath.row];
    CYPayBackTableVC *tab = [[CYPayBackTableVC alloc] init];
    tab.dic = a;
    [self.navigationController pushViewController:tab animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSDictionary *a = self.datas[section];
    CYLoanSectionView *sectionView = [CYLoanSectionView sectionView];
    sectionView.delegate = self;
    sectionView.section = section;
    sectionView.productNameLabel.text = a[@"productName"];
    sectionView.loanMoneyLabel.text = [NSString stringWithFormat:@"%@", a[@"loanMoney"]];
    sectionView.payBackBtn.tag = section;
    [sectionView.payBackBtn addTarget:self action:@selector(payBackBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return sectionView;
}

- (void)payBackBtnClick:(UIButton *)btn {
    NSDictionary *a = self.datas[btn.tag];
    CYPayBackTableVC *tab = [[CYPayBackTableVC alloc] init];
    tab.dic = a;
    [self.navigationController pushViewController:tab animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0;
}

#pragma mark - CYLoanSectionViewDelegate
- (void)openSection: (CYLoanSectionView *)sectionHeaderView atPosition:(NSInteger)position {
    sectionHeaderView.cartBtn.selected = YES;
    
    NSMutableArray *indexPathsToInsert = [NSMutableArray array];
    NSMutableArray *indexPathsToDelete = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 1; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:position]];
    }
    
    if (self.openedSectionView != nil) {
        self.openedSectionView.cartBtn.selected = NO;
        NSInteger deleteRowCount = [self.tableView numberOfRowsInSection:self.openedSectionView.section];
        for (int i = 0; i < deleteRowCount; ++i) {
            [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:self.openedSectionView.section]];
        }
    }
    
    UITableViewRowAnimation insertAnimation;
    UITableViewRowAnimation deleteAnimation;
    
    if (self.openedSectionView == nil || position < self.openedSectionView.section) {
        insertAnimation = UITableViewRowAnimationTop;
        deleteAnimation = UITableViewRowAnimationBottom;
    } else {
        insertAnimation = UITableViewRowAnimationBottom;
        deleteAnimation = UITableViewRowAnimationTop;
    }
    
    [self.tableView beginUpdates];
    if (indexPathsToInsert.count > 0) {
        [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
    }
    if (indexPathsToDelete.count > 0) {
        [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
    }
    [self.tableView endUpdates];
    
    self.openedSectionView = sectionHeaderView;
}

- (void)closeSection: (CYLoanSectionView *)sectionHeaderView atPosition:(NSInteger)position {
    sectionHeaderView.cartBtn.selected = NO;
    
    NSInteger numberOfRowsToDelete = [self.tableView numberOfRowsInSection:position];
    
    NSMutableArray *indexPathsToDelete = [NSMutableArray array];
    for (int i = 0; i < numberOfRowsToDelete; ++i) {
        [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:position]];
    }
    
    UITableViewRowAnimation deleteAnimation = UITableViewRowAnimationTop;
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
    [self.tableView endUpdates];
    self.openedSectionView = nil;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
